<template>
  <van-nav-bar placeholder fixed border>
    <template #left v-if="showLeft">
      <img @click="clickLeft" class="left_back" src="@/assets/back.png" alt="">
    </template>
    <template #right>
      <img src="@/assets/search.png" alt="" class="right_search" @click.stop="emit('click-right-search')"
           v-if="showSearch">
      <img src="@/assets/user.png" alt="" class="right_user" @click.stop="navToUser">
    </template>
  </van-nav-bar>
</template>

<script setup>
import * as dd from 'dingtalk-jsapi'; // 此方式为整体加载，也可按需进行加载

import {useRouter, useRoute} from 'vue-router'
import {onMounted, watch} from "vue";
import {login} from "../../api/index.js";
import {getToken, setInfo} from "../../localStorage/index.js";

const route = useRoute()
const router = useRouter()

const props = defineProps({
  title: {
    type: String,
    default: "东方神韵线上展厅"
  },
  showSearch: {
    type: Boolean,
    default: false
  },
  showLeft: {
    type: Boolean,
    default: true
  }
})
const demo = {
  code: 1,
  msg: "ok",
  time: "1663771248",
  data: {
    userinfo: {
      id: 16,
      username: "1663668354gTfF",
      nickname: "李文奇",
      mobile: "",
      avatar: "/assets/img/avatar.png",
      score: 0,
      token: "9da5a34a-762c-4bb0-8783-8a8e483ff30b",
      user_id: 16,
      createtime: 1663771248,
      expiretime: 1666363248,
      expires_in: 2592000
    }
  }
}
onMounted(() => {

  console.log(dd, "dd")
  dd.ready(function () {
    // dd.ready参数为回调函数，在环境准备就绪时触发，jsapi的调用需要保证在该回调函数触发后调用，否则无效。
    dd.runtime.permission.requestAuthCode({
      corpId: "ding61e139862c9007f635c2f4657eb6378f", // 企业id
      onSuccess: function (info) {
        // console.log(info, "info")
        if(!getToken()){
          // window.localStorage.setItem("")
          login({
            code: info.code
          }).then(res => {
            console.log(res, "res")

            setInfo(res.data.userinfo)
            window.location.reload()

          })
        }

        // code = info.code // 通过该免登授权码可以获取用户身份
      },
      onFail: function (err) {
        console.log(err, "err")
      }

    });
  });

})
watch(() =>router.currentRoute.value.path, () => {
  console.log(props.title)
  document.title = props.title;
},{ immediate: true })
const clickLeft = () => {
  // console.log(this.router)
  router.back()
}
const navToUser = () => {
  router.push("/My")
}
const emit = defineEmits(["click-right-user", "click-right-search"])


</script>

<style scoped>
.left_back {
  width: 30px;
}

.right_search {
  width: 20px;
  margin-right: 10px;
}

.right_user {
  width: 20px;
}
</style>
